<?php
namespace app\index\controller;

use app\index\model\Earnings;
use think\Request;
use think\Session;
use app\index\model\Channel;
use app\index\model\Visits;
use app\index\model\Courses;

class ChannelList extends Common
{
    public function index()
    {
        $channelModel = new Channel();
        $data = $channelModel->getAllLists([
            'user_id' => Session::get('cps_user_id')
        ]);
        $this->assign('header_name', '渠道列表');
        $this->assign('header_info', '（点击查看收益详情，数据有1-2小时延迟）');
        $this->assign('lists', $data['lists']);
        $this->assign('page', $data['page']);
        return $this->fetch();
    }

    //详情
    public function details(Request $request)
    {
        //P($request->param());
        $channel_id = $request->param('channel_id', 0, 'intval');
        $search_type = $request->param('type', 'today', 'filterString');  //查询类型
        if (!$channel_id) {
            $this->error('渠道ID错误');
        }
        $channelModel = new Channel();
        $channel_info = $channelModel->getInfoByWhere([
            'id' => $channel_id,
            'user_id' => Session::get('cps_user_id')
        ]);
        if (!$channel_info) {
            $this->error('渠道ID错误');
        }
        if ($request->param('starttime')) {
            $this->assign('starttime', $request->param('starttime', '', 'filterString'));
        }
        if ($request->param('endtime')) {
            $this->assign('endtime', $request->param('endtime', '', 'filterString'));
        }
        $visits = new Visits();
        $earnings = new Earnings();
        $coursesModel = new Courses();
        $visits_data = $visits->statsByTime($search_type);      //访问量统计
        $earnings_data = $earnings->statsByTime($search_type);  //收益统计
        $this->assign('channel_id', $channel_id);
        $this->assign('header_name', $coursesModel->getInfoByField('id', $channel_info['course_id'], 'title')->title);
        $this->assign('type', $visits_data['type']);
        $this->assign('visits_data', $visits_data['data']);
        $this->assign('earnings_data', $earnings_data['data']);
        return $this->fetch();
    }

    //集合详情
    public function gather_details()
    {
        return $this->fetch('', [
            'header_name' => '推广集合名称',
        ]);
    }

}
